package org.springframework.aop.org.springframework.aop.framework.adapter;

import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.org.springframework.aop.MethodBeforeAdvice;

/**
 * @author admin
 */
public class MethodBeforeAdviceInterceptor implements MethodInterceptor, AdviceAdapter {

    private MethodBeforeAdvice methodBeforeAdvice;

    @Override
    public boolean support(Advice advice) {
        return advice instanceof MethodBeforeAdvice;
    }

    @Override
    public MethodInterceptor getInterceptor(Advice advice) {
        if (!support(advice)) {
            throw new RuntimeException("参数化错误");
        }
        return new MethodBeforeAdviceInterceptor((MethodBeforeAdvice) advice);
    }

    public MethodBeforeAdviceInterceptor(MethodBeforeAdvice methodBeforeAdvice) {
        this.methodBeforeAdvice = methodBeforeAdvice;
    }

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        methodBeforeAdvice.before(invocation.getMethod(), invocation.getArguments(), invocation.getThis());
        return invocation.proceed();
    }
}
